var newFilehandler = function(guiObject) {
	var id = guiObject.id,
		filename = common.formatFilname(guiObject.form.file.value),
		domain = common.getDomain(),
		protocol = common.getProtocol(),
		abort = false,
		getStatus = function() {
			$.getJSON(common.createPath({id : id, op : "gs"}), function(data) {
				var progress;
				
				if(abort) {
					return;
				}
								
				switch(data.state) {
				case data.WAITING_FOR_PEER:
					console.log("WAITING_FOR_PEER");
					guiObject.progresstext.innerHTML = "Waiting for peer...";
					common.wait(getStatus, 1000);
					break;
				case data.WAITING_FOR_SIZE:
					console.log("WAITING_FOR_SIZE");
					guiObject.form.submit();
					guiObject.progresstext.innerHTML = "Waiting for size calculation...";
					common.wait(getStatus, 1000);
					break;
				case data.SENDING:
					console.log("SENDING");
					guiObject.progresstext.innerHTML = common.formatSize(data.bytesSent);
					guiObject.progresstext.innerHTML += " / ";
					guiObject.progresstext.innerHTML += common.formatSize(data.totalBytesToSend);
					progress = data.bytesSent*100/data.totalBytesToSend;
					$(guiObject.progressbar).progressbar({ value: progress });
					common.wait(getStatus, 1000);
					break;
				case data.DONE:
					console.log("DONE");
					guiObject.progresstext.innerHTML = "Done";
					$(guiObject.progressbar).progressbar({ value: 100 });
					common.removeItem(guiObject.iframe);
					common.wait(function() {
						common.removeItem(guiObject.item);
					}, 5000);
					break;
				case data.ERROR:
					common.removeItem(guiObject.iframe);
					common.removeItem(guiObject.item);
					alert(data.msg);
					break;
				default:
					common.removeItem(guiObject.iframe);
					common.removeItem(guiObject.item);
					alert("Unknown state " + data.state);
				}
			});
		};
	
	$(guiObject.progressbar).progressbar({ value: 0 });
	guiObject.progresstext.innerHTML = "Waiting for peer...";
	guiObject.linktext.innerHTML = protocol + "//" + domain + common.createPath({id : id, op : "dl", filename : filename});
	
	$(guiObject.abort).click(function(event) {
		abort = true;
		common.removeItem(guiObject.iframe);
		common.removeItem(guiObject.item);
	});
	
	getStatus();
}